package com.jzsm.modules.sender.senderService.impl.email.utils;

import com.alibaba.fastjson.JSON;
import com.jzsm.common.utils.StringUtils;
import com.jzsm.modules.sender.bo.accountConfig.EmailaccountConfigBo;
import com.jzsm.modules.sender.vo.MailMessageVo;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;

import java.util.Objects;
import java.util.Properties;

public class EmailUtils {
    public static JavaMailSenderImpl makeMailSender(EmailaccountConfigBo config) {
        final JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
        javaMailSender.setHost(config.getHost());
        if (StringUtils.isNotBlank(config.getProtocol())) {
            javaMailSender.setProtocol(config.getProtocol());
        }
        if (StringUtils.isNotBlank(config.getDefaultEncoding())) {
            javaMailSender.setDefaultEncoding(config.getDefaultEncoding());
        }
        if (Objects.nonNull(config.getPort())) {
            javaMailSender.setPort(config.getPort());
        }
        javaMailSender.setUsername(config.getUsername());
        javaMailSender.setPassword(config.getPassword());
        Properties properties = new Properties();
        properties.put("mail.smtp.auth", true);
//        properties.put("mail.smtp.ssl.enable", true);
        properties.put("mail.smtp.starttls.enable", true);
        properties.put("mail.smtp.starttls.required", true);
//        properties.put("mail.smtp.host", config.getHost());
        javaMailSender.setJavaMailProperties(properties);
        return javaMailSender;
    }

}
